use Mojo::mysql;
use Mojo::JSON qw(decode_json encode_json);
use strict;

my $mysql = Mojo::mysql->strict_mode('mysql://dahama:xxxxxxxxxx@/vpninfo');

my $db = $mysql->db;

sub show_mac_info {
    my $user = shift; #从Mojo的session中获取
    my $company_id = shift;
    my $user_right = $db->query('select sbu_id,userrole from admin_account where username=?',$user)->array;
    my $company_right = $db->query('select company_id from sbu_company_relation where sbu=? and role=?',$user_right->[0],$user_right->[1])->array;
    if (!$company_right->[0]) {
        print "对不起，没有你要找的公司"; #在Mojo中使用render渲染
    }else{
        my $result = $db->query('select mac,tibaoren,shijian,guanliyuanshenhe,chuli,xitongchuli,chuliyijian from maclist where company=?',$company_id)->hashes;
        print "$result->[0]->{mac}\n"; #在Mojo中渲染为json后直接发出
    }
}

sub add_mac_info {
    my $user = shift; #从Mojo的session中获取
    my $info = shift; #从前端接受JSON信息
    my $hash = decode_json $info;
    my $user_right = $db->query('select sbu_id,userrole from admin_account where username=?',$user)->array;

    #此处建议添加代码，检查前端转化的JSON正确性

    for my $key (keys %{$hash}) {
        my $company_id = $hash{$key}->{company};
        my $company_right = $db->query('select company_id from sbu_company_relation where sbu=? and role=?',$user_right->[0],$user_right->[1])->array;
        my $user_count = $db->query('select username from vpn_account where company_id=?',$company_id)->array;
        my $u_count = $#{$user_count}+1;
        my $mac_count = $db->query('select mac from maclist where company=?',$company_id)->array;
        if (!$company_right->[0]){
            print "对不起，编号为：$company_id 的这家公司未注册！"; #在Mojo中使用render渲染
        }else{
            $@ = '';
            eval{
                my $tx = $db->begin;
                my $count = 3*$u_count-($#{$mac_count}+1);
                for $mac (@{$hash{$key}->{macs}}) {
                    if($count <= 0){
                      print "对不起，编号为： $company_id 的这家公司MAC地址 $mac 因数量已达到最大值，不允许再增加\n"; #在Mojo中使用render渲染；
                    }else{
                      $db->query('insert into maclist (company,mac,shijian,chuli,tibaoren,guanliyuanshenhe,xitongchuli) values (?,?,now(),0,?,0,0)',$company_id,$mac,$user);
                    }
                }
                $tx->commit;
                --$count;
            };
            if $@ {
                print $@;
            }else{
                print "编号为 $company_id 的公司MAC地址已提交"； #在Mojo中render渲染
            }
        }
    }
}

sub delete_mac_info {
    my $user = shift; #从Mojo中的session获取
    my @del_macs = shift; #获取待删除MAC地址的数组
    
    $@ = '';
    eval{
        my $tx = $db->begin;
        for my $mac (@del_macs) {
            my $admin_info = $db->query('select guanliyuanshenhe from maclist where mac=?',$mac)->array;
            if ($admin_info->[0] == 0 or $admin_info->[0] == 2) {
                $db->query('update maclist set guanliyuanshenhe=1,chuli=0,xitongchuli=1');
            }else{
                $db->query('update maclist set guanliyuanshenhe=0,chuli=0,xitongchuli=0');
            }
        }
        $tx->commit;
    };
    if $@ {
        print $@;
    }else{
        print "删除成功，等待管理员审核"; #在Mojo中使用render渲染
    }
}